package edu.cuit.canary;

import java.util.Scanner;

/**
 * 计算贷款支付程序
 * @author 小猪Peppa
 */
public class ComputeLoan {
    public static void main(String[] args) {
        //step1：提示用户输入贷款利率、贷款额度、贷款年限
        try(Scanner input = new Scanner(System.in)){
            System.out.print("请输入贷款额度:");
            int loanAmount = input.nextInt();
            System.out.print("请输入贷款年限:");
            int year = input.nextInt();
            System.out.print("请输入贷款年利率(格式为：4.5，代表年利率为4.5%):");
            float interestRate = input.nextFloat();

            //step2：对于年利率的输入是一个百分比格式的数字，需要转换为十进制数，输入的利率是年利率，可以计算出月利率
            float monthlyInterestRate = interestRate / 100 / 12;

            //step3: 使用公式计算月支付额
            float monthlyPayment = (float)(loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, year * 12)));

            //step4：计算总支付额
            float totalPayment = monthlyPayment * 12 * year;

            System.out.println("贷款额度\t贷款利率\t贷款年限");
            System.out.println(loanAmount + "\t" + interestRate + "%\t" + year);
            System.out.println("月支付额:" + monthlyPayment);
            System.out.println("总支付额:" + totalPayment);
        }

    }
}
